package chess;

import game.Game;

public class TestChessGamePlayer extends Player {

	private int gameTurn;
	private Tile[] moveTiles, destTiles;
	private int myTestType;
	public static final int CHECKMATE_TEST = 1;
	public static final int STALEMATE_TEST = 2;
	public static final int CASTLE_TEST = 3;
	public static final int CHECK_TEST = 4;

	public TestChessGamePlayer(String name, Game game, String colour) {
		super(name, game, colour);
		gameTurn = 0;

		moveTiles = new Tile[10];
		destTiles = new Tile[10];

	}

	public void setTestType(int type) {
		myTestType = type;
	}

	/**
	 * Creates the shortest number of moves for each player. This provides us
	 * with a way to test the play method in ChessGameTest.
	 * 
	 * <pre>
	 * Samual Martelli
	 * -Creation (Feb 24,2010)
	 * </pre>
	 */
	public void createPlayersMoves() {
		if (getColour().equals("White") && myTestType == CHECKMATE_TEST) {
			// Set moves

			// Bad pawn move
			moveTiles[0] = currentGame.getBoard().getTile(6, 6);
			destTiles[0] = currentGame.getBoard().getTile(6, 4);

			// another bad pawn move
			moveTiles[1] = currentGame.getBoard().getTile(5, 6);
			destTiles[1] = currentGame.getBoard().getTile(5, 4);
		} else if (getColour().equals("Black") && myTestType == CHECKMATE_TEST) {
			// Set Moves

			// Getting ready...
			moveTiles[0] = currentGame.getBoard().getTile(4, 1);
			destTiles[0] = currentGame.getBoard().getTile(4, 2);

			// Owned
			moveTiles[1] = currentGame.getBoard().getTile(3, 0);
			destTiles[1] = currentGame.getBoard().getTile(7, 4);
		} else if (getColour().equals("White") && myTestType == STALEMATE_TEST) {
			// Set moves to force a stalemate
			moveTiles[0] = currentGame.getBoard().getTile(2, 6);
			destTiles[0] = currentGame.getBoard().getTile(2, 4);

			moveTiles[1] = currentGame.getBoard().getTile(7, 6);
			destTiles[1] = currentGame.getBoard().getTile(7, 4);

			moveTiles[2] = currentGame.getBoard().getTile(3, 7);
			destTiles[2] = currentGame.getBoard().getTile(0, 4);

			moveTiles[3] = currentGame.getBoard().getTile(0, 4);
			destTiles[3] = currentGame.getBoard().getTile(0, 3);

			moveTiles[4] = currentGame.getBoard().getTile(0, 3);
			destTiles[4] = currentGame.getBoard().getTile(2, 1);

			moveTiles[5] = currentGame.getBoard().getTile(2, 1);
			destTiles[5] = currentGame.getBoard().getTile(3, 1);

			moveTiles[6] = currentGame.getBoard().getTile(3, 1);
			destTiles[6] = currentGame.getBoard().getTile(1, 1);

			moveTiles[7] = currentGame.getBoard().getTile(1, 1);
			destTiles[7] = currentGame.getBoard().getTile(1, 0);

			moveTiles[8] = currentGame.getBoard().getTile(1, 0);
			destTiles[8] = currentGame.getBoard().getTile(2, 0);

			// stalemate move everyone loses the game!
			moveTiles[9] = currentGame.getBoard().getTile(2, 0);
			destTiles[9] = currentGame.getBoard().getTile(4, 2);
		} else if (getColour().equals("Black") && myTestType == STALEMATE_TEST) {
			// Set moves to force a stalemate
			moveTiles[0] = currentGame.getBoard().getTile(7, 1);
			destTiles[0] = currentGame.getBoard().getTile(7, 3);

			moveTiles[1] = currentGame.getBoard().getTile(0, 1);
			destTiles[1] = currentGame.getBoard().getTile(0, 3);

			moveTiles[2] = currentGame.getBoard().getTile(0, 0);
			destTiles[2] = currentGame.getBoard().getTile(0, 2);

			moveTiles[3] = currentGame.getBoard().getTile(0, 2);
			destTiles[3] = currentGame.getBoard().getTile(7, 2);

			moveTiles[4] = currentGame.getBoard().getTile(5, 1);
			destTiles[4] = currentGame.getBoard().getTile(5, 2);

			moveTiles[5] = currentGame.getBoard().getTile(4, 0);
			destTiles[5] = currentGame.getBoard().getTile(5, 1);

			moveTiles[6] = currentGame.getBoard().getTile(3, 0);
			destTiles[6] = currentGame.getBoard().getTile(3, 5);

			moveTiles[7] = currentGame.getBoard().getTile(3, 5);
			destTiles[7] = currentGame.getBoard().getTile(7, 1);

			moveTiles[8] = currentGame.getBoard().getTile(5, 1);
			destTiles[8] = currentGame.getBoard().getTile(6, 2);

			// move cannot be done should be stalemate at this point
			moveTiles[9] = currentGame.getBoard().getTile(6, 1);
			destTiles[9] = currentGame.getBoard().getTile(6, 1);
		} else if (getColour().equals("White") && myTestType == CASTLE_TEST) {
			// Set moves

			// move the horse out charge!
			moveTiles[0] = currentGame.getBoard().getTile(6, 7);
			destTiles[0] = currentGame.getBoard().getTile(5, 5);

			// move the pawn out charge!
			moveTiles[1] = currentGame.getBoard().getTile(4, 6);
			destTiles[1] = currentGame.getBoard().getTile(4, 5);
			// move the bishop out charge!
			moveTiles[2] = currentGame.getBoard().getTile(5, 7);
			destTiles[2] = currentGame.getBoard().getTile(3, 5);
			// king protection(castle)!
			moveTiles[3] = currentGame.getBoard().getTile(4, 7);
			destTiles[3] = currentGame.getBoard().getTile(6, 7);
			// another move the pawn out charge!
			moveTiles[4] = currentGame.getBoard().getTile(2, 6);
			destTiles[4] = currentGame.getBoard().getTile(2, 5);
		} else if (getColour().equals("Black") && myTestType == CASTLE_TEST) {
			// move the horse out charge!
			moveTiles[0] = currentGame.getBoard().getTile(1, 0);
			destTiles[0] = currentGame.getBoard().getTile(2, 2);

			// move the pawn out charge!
			moveTiles[1] = currentGame.getBoard().getTile(3, 1);
			destTiles[1] = currentGame.getBoard().getTile(3, 2);
			// move the bishop out charge!
			moveTiles[2] = currentGame.getBoard().getTile(2, 0);
			destTiles[2] = currentGame.getBoard().getTile(4, 2);
			// move the queen charge!
			moveTiles[3] = currentGame.getBoard().getTile(3, 0);
			destTiles[3] = currentGame.getBoard().getTile(3, 1);
			// king protection(castle)!
			moveTiles[4] = currentGame.getBoard().getTile(4, 0);
			destTiles[4] = currentGame.getBoard().getTile(2, 0);
		}
		else if (getColour().equals("White") && myTestType == CHECK_TEST) {
			// Set moves

			// move the pawn out charge!
			moveTiles[0] = currentGame.getBoard().getTile(4, 6);
			destTiles[0] = currentGame.getBoard().getTile(4, 5);

			// move the pawn out charge!
			moveTiles[1] = currentGame.getBoard().getTile(5, 6);
			destTiles[1] = currentGame.getBoard().getTile(5, 4);
			// bishop check!
			moveTiles[2] = currentGame.getBoard().getTile(5, 7);
			destTiles[2] = currentGame.getBoard().getTile(1, 3);
			// put king in jeopardy!
			moveTiles[3] = currentGame.getBoard().getTile(4, 7);
			destTiles[3] = currentGame.getBoard().getTile(4, 6);
			// another move the pawn out charge!
		}
		else if (getColour().equals("Black") && myTestType == CHECK_TEST) {
			// Set moves

			// move the pawn out charge!
			moveTiles[0] = currentGame.getBoard().getTile(5, 1);
			destTiles[0] = currentGame.getBoard().getTile(5, 2);

			// move the pawn out charge!
			moveTiles[1] = currentGame.getBoard().getTile(3, 1);
			destTiles[1] = currentGame.getBoard().getTile(3, 2);
			// move king out of check!
			moveTiles[2] = currentGame.getBoard().getTile(4, 0);
			destTiles[2] = currentGame.getBoard().getTile(5, 1);
			// check the other king as revenge!
			moveTiles[3] = currentGame.getBoard().getTile(2, 0);
			destTiles[3] = currentGame.getBoard().getTile(6, 4);
		}
	}

	public void takeTurn() {
		command.MoveCommand move = new command.MoveCommand(moveTiles[gameTurn],
				destTiles[gameTurn]);
		this.currentGame.listOfMoves.add(move);
		move.execute();
		if (myTestType == CASTLE_TEST) {
			if (getColour().equals("Black") && gameTurn == 4) {
				game.ChessGame cg = (game.ChessGame) currentGame;
				cg.finishCastleMove(this, destTiles[gameTurn]);
			} else if (getColour().equals("White") && gameTurn == 3) {
				game.ChessGame cg = (game.ChessGame) currentGame;
				cg.finishCastleMove(this, destTiles[gameTurn]);
			}
		}
		gameTurn++;
		if (myTestType == CASTLE_TEST) {
			if (gameTurn == 5 && getColour().equals("Black")) {
				currentGame.newGameSelected(); // Force the game to end
			}
		}
		else if (myTestType == STALEMATE_TEST) {
			if (gameTurn == 1 && getColour().equals("Black")) {
				currentGame.newGameSelected(); // Force the game to end
			}
		}
		else if (myTestType == CHECK_TEST) {
			if (gameTurn == 3 && getColour().equals("Black")) {
				currentGame.newGameSelected(); // Force the game to end
			}
		}
		

	}

}
